package com.game.element
{
	import com.game.manager.ActionManager;
	import com.game.manager.LayerManager;
	import com.interfaces.general.mangaer.ITick;
	
	import flash.display.Stage;
	import flash.geom.Point;
	import flash.utils.getTimer;

	public class BaseWeapon implements ITick
	{
		private var lastCheckTime:Number =0;
		protected var fireInterval :Number = 1000/3;//开枪的频率
		private var fired:Boolean =  false;
		public var playerElement:PlayerElement;
		
		public var totalBullets:int = -1;//总共有多少发子弹
		public var currentBullets:int = 12;//当前弹夹里面的子弹数量
		public var oneClipLimits :int = 12;//一个弹夹容量的子弹
		public var reloadNeedTime:Number = 1000;//换子弹需要的时间
		public var impactForce :Number = 40;//击退力度
		
		public function BaseWeapon()
		{
		}
		
		public function fire(startPoint:Point,targetPoint:Point):void{
			if(playerElement.startReloading > 0){//正在换子弹){
				return;
			}
			if(currentBullets <=0){
				currentBullets = 0;
				return;
			}
			var bullet:Bullet = new Bullet;
			bullet.x = startPoint.x;
			bullet.y = startPoint.y;
			bullet.speed = 400;
			bullet.damage = int(Math.random()*10+10);
			bullet.impactForce = impactForce;
			bullet.fireMan = playerElement;
		
			var p:Point = targetPoint.subtract(startPoint);
			p.normalize(200);
			var d:Point = startPoint.add(p);
			
			bullet.targetPoint = d;
			
			LayerManager.sceneView.addBullets(bullet);
			ActionManager._allBullets.push(bullet);
			
			currentBullets--;
			if(currentBullets <=0){
				stopFire();
//				playerElement.reloadClip(reloadNeedTime);
			}
		}
		
		public function reloadClipComplete():void{
			if(totalBullets != -1){
				if(totalBullets < oneClipLimits){
					currentBullets = totalBullets;
					totalBullets = 0;
				}
				totalBullets -= oneClipLimits;
			}
			currentBullets = oneClipLimits;
		}
		
		public function startFire():void{
			fired = true;
		}
		
		public function stopFire():void{
			fired = false;
		}
		
		public function exce():void
		{
			if(fired == false){
				return;
			}
			if(lastCheckTime >0 && getTimer() -lastCheckTime < fireInterval){
				return;
			}
			lastCheckTime = getTimer();
			fire(new Point(playerElement.x,playerElement.y),new Point(LayerManager.sceneView.mouseX,LayerManager.sceneView.mouseY));
		}
		
		public function set tickPriority(priority:int):void
		{
		}
		
		public function get tickPriority():int
		{
			return 0;
		}
	}
}